//
//  CGQshebeiInfoVC.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/8.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then
import SDCycleScrollView

class CGQshebeiInfoVC: SYBaseTableViewController {
    
    fileprivate var vm: VGTableViewPageViewModel<CGQshebeiinfoModel>!
    var  model: CGQshebeiinfoModel!
    
    
    var id: String!
    
    init(id: String) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var delete: UIButton = {
        let v = UIButton()
        v.setTitle("删除", for: .normal)
        v.setTitleColor(UIColor(hex: 0x0782FF), for: .normal)
        v.titleLabel?.font = 18.autoFontSize()
        v.layer.cornerRadius = 3
        v.layer.masksToBounds = true
        v.backgroundColor = .white
        v.isHidden = true
        return v
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        loadData()
        
        super.viewWillAppear(animated)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        setNoDataView()
        setRightNavItem()
        self.title = "设备信息"
        self.view.backgroundColor = Constant.bgViewColor
    }
    
    func setRightNavItem() {
        let item: UIBarButtonItem = UIBarButtonItem(title: "编辑", style: UIBarButtonItem.Style.plain, target: self, action: #selector(rightAction))
        self.navigationItem.rightBarButtonItem=item
    }
    
    @objc func rightAction() {
        let vc = CGQshebeiInfoEditVC(id: self.id)
        self.sy_push(vc)
    }
    
    func loadData() {
        
        requestVM.baseRequest(disposeBag: dispose, type: .sheDetail(id: self.id), modelClass: SGBaseModel<CGQshebeiinfoModel>.self) {[weak self] (res) in
            
            if let `self` = self  {
                self.model = res.data ?? CGQshebeiinfoModel()
                self.loadDataSuccessed()
            }
            
        } Error: {
            
        }
    }
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            
            self.setDataSource()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = .clear
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)
        
        tableView.rx.itemSelected
            .subscribe(onNext:{ [weak self] indexPath in
                if let `self` = self {
                    
                }
            })
            .disposed(by:dispose)
        
        delete.frame = CGRect(x: 10.autoWidth(), y: SCREEN_HEIGHT - 64.autoWidth() - BOTTOM_SAFE_HEIGHT, width: SCREEN_WIDTH - 20.autoWidth(), height: 44.autoWidth())
        self.view.addSubview(delete)
        
    }
    
    func setNoDataView() {
        noDataView = SYNoDataView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT))
        noDataView.isHidden = true
        tableView.addSubview(noDataView)
    }
    
    func setDataSource() {
        //初始化数据
        let sections = Observable.just([
            CGQshebeiinfoModelSection(header: "", items: [CGQshebeiinfoModel(), CGQshebeiinfoModel(),CGQshebeiinfoModel(),CGQshebeiinfoModel(),CGQshebeiinfoModel(),CGQshebeiinfoModel(),CGQshebeiinfoModel(),CGQshebeiinfoModel(),CGQshebeiinfoModel()])//self.vm.dataSource)
        ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<CGQshebeiinfoModelSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                
                let cell: CGQshebeiInfoCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                    as? CGQshebeiInfoCell ?? CGQshebeiInfoCell(style: .default, reuseIdentifier: "Cell")
                cell.configureView(model: self.model,row: ip.row)
                cell.selectionStyle = .none
                return cell
                
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
    }
    
    
}


//自定义Section
struct CGQshebeiinfoModelSection {
    var header: String
    var items: [Item]
}

extension CGQshebeiinfoModelSection : AnimatableSectionModelType {
    typealias Item = CGQshebeiinfoModel
    
    var identity: String {
        return header
    }
    
    init(original: CGQshebeiinfoModelSection, items: [Item]) {
        self = original
        self.items = items
    }
}

extension CGQshebeiinfoModel: IdentifiableType {
    var identity: String {
        return id!
    }
    
    typealias Identity = String
}

